package com.yc.web;

import com.yc.damai.entity.DmUser;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author navy
 * @company 源辰信息
 * @date 2024/9/21
 * @email haijunzhou@hnit.edu.cn
 */
@RestController
public class OtherDo {
    @RequestMapping("other")
    String other(HttpServletRequest request){
        return "other " + request.getServerPort();
    }

    @RequestMapping("hello")
    String hello(String name){
        return "hello " + name;
    }

    @RequestMapping("add")
    int add(int a, int b){
        return a + b;
    }

    @RequestMapping("age/{num}")
    String age(@PathVariable int num){
        return "我" + num + "岁了!";
    }

    @RequestMapping("resetPwd")
    DmUser resetPwd(@RequestBody DmUser user,
                    String pwd,
                    String repwd){
       if(pwd.equals(repwd)){
           user.setPassword(pwd);
       }
       return user;
    }

    @RabbitListener(queuesToDeclare = @Queue("workQueue"))
    public void getWorkMsg1(String data){
        System.out.println("data1 = " + data);
    }

    @RabbitListener(queuesToDeclare = @Queue("workQueue"))
    public void getWorkMsg2(String data){
        System.out.println("data2 = " + data);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(value="fanoutEx", type = ExchangeTypes.FANOUT)
    ))
    public void fanout1(String data){
        System.out.println("广播模式1 = " + data);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(value="fanoutEx", type = ExchangeTypes.FANOUT)
    ))
    public void fanout2(String data){
        System.out.println("广播模式2 = " + data);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(value="directEx", type = ExchangeTypes.DIRECT),
            key = "error"
    ))
    public void direct1(String data){
        System.out.println("定向模式1 = " + data);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(value="directEx", type = ExchangeTypes.DIRECT),
            key = "info"
    ))
    public void direct2(String data){
        System.out.println("定向模式2 = " + data);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(value="topicEx", type = ExchangeTypes.TOPIC),
            key = {"user.*","order.#"}
    ))
    public void topic1(String data){
        System.out.println("主题模式1 = " + data);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,
            exchange = @Exchange(value="topicEx", type = ExchangeTypes.TOPIC),
            key = {"user.#","order.*"}
    ))
    public void topic2(String data){
        System.out.println("主题模式2 = " + data);
    }

}
